package JAVA300.第十二章WebServer;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.swing.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * 熟悉XML SAX
 *解析
 */
public class server2_XML01 {
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        //SaX解析
        //1，获取解析工厂
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2,从解析工厂获取解析器
        SAXParser sparse;
        sparse= factory.newSAXParser();


        //3 自己编写处理器
        //4 加载文档Document 注册解析器

        PersonHandler handler = new PersonHandler();
        //5解析
        sparse.parse(Thread.currentThread().getContextClassLoader().
                getResourceAsStream("D:\\Java300_Exp\\src\\JAVA300\\第十二章WebServer\\p.xml")
                ,handler);

    }


}

class PersonHandler extends DefaultHandler{
    @Override
    public void startDocument() throws SAXException {
        System.out.println("开始解析文件");
    }

    @Override
    public void endDocument() throws SAXException {
        System.out.println("结束解析文件");
    }

    //解析元素
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        //只关注qName--元素名/标签名
        System.out.println(qName+"--->解析开始");
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        System.out.println(qName+"--->解析结束");
    }
}
